package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.*;
import com.sg.entity.OmsMessageMember;
import com.sg.service.base.component.MOmsMessageMemberService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsMessageMemberMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsMessageMemberServiceImpl
        extends ServiceImpl<OmsMessageMemberMapper, OmsMessageMember>
        implements MOmsMessageMemberService {

    @Autowired
    OmsMessageMemberMapper omsMessageMemberMapper;

    /**
     * 2-5生成成员消息接收[4006]
     * gen by moon at 8/15/2023, 8:32:01 PM
     */
    @Trace(operationName = "2-5生成成员消息接收")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String generateMemberMsgReceive(OmsMessageMember omsMessageMember) {
        omsMessageMember.setMemberMessageId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsMessageMember.setIsValid("FALSE");
        if (super.save(omsMessageMember)) {
            return omsMessageMember.getMemberMessageId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-5-03查询成员消息接收列表[4029]
     * gen by moon at 8/15/2023, 8:32:02 PM
     */
    @Trace(operationName = "2-5-03查询成员消息接收列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageMember> queryMemberMsgReceiveList(QueryMemberMsgReceiveListReq reqDto) {

        try {
            QueryWrapper<OmsMessageMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            // TODO: 2023/5/17 手写代码 
            if (reqDto.getMessageImportantLevel() != null) {
                queryWrapper.gt("message_important_level", reqDto.getMessageImportantLevel());
                reqDto.setMessageImportantLevel(null);
            }

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsMessageMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-03修改成员消息接收[4031]
     * gen by moon at 5/17/2023, 3:40:46 AM
     */
    @Trace(operationName = "2-5-03修改成员消息接收")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMemberMsgReceive(OmsMessageMember omsMessageMember) {
        omsMessageMember.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsMessageMember);
    }

    /**
     * 2-5-03批量增成员消息接收[7016]
     * gen by moon at 8/15/2023, 8:32:02 PM
     */
    @Trace(operationName = "2-5-03批量增成员消息接收")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddMemberMsgReceive(List<OmsMessageMember> omsMessageMember) {

        if (omsMessageMember == null || omsMessageMember.size() == 0) {
            return false;
        }

        return super.saveBatch(omsMessageMember.stream().map(item -> {
            item.setMemberMessageId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsValid("FALSE");
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 2-5-03删除成员消息接收[7042]
     * gen by moon at 5/31/2023, 6:08:11 PM
     */
    @Trace(operationName = "2-5-03删除成员消息接收")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteMemberMsgReceive(String memberMessageId) {

        return super.removeById(memberMessageId);
    }

    /**
     * 2-5-03查询成员消息接收详情[7057]
     * gen by moon at 5/31/2023, 7:59:14 PM
     */
    @Trace(operationName = "2-5-03查询成员消息接收详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsMessageMember queryMemberMsgReceiveDetail(QueryMemberMsgReceiveDetailReq reqDto) {

        try {


            QueryWrapper<OmsMessageMember> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;

            // TODO: 2023/12/15 手写代码
            if (reqDto.getMessageImportantLevel() != null) {
                queryWrapper.gt("message_important_level", reqDto.getMessageImportantLevel());
                reqDto.setMessageImportantLevel(null);
            }
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsMessageMember> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsMessageMember retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-03批量改成员消息接收[7362]
     * gen by moon at 8/6/2023, 6:13:52 PM
     */
    @Trace(operationName = "2-5-03批量改成员消息接收")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateMemberMsgReceive(List<OmsMessageMember> omsMessageMember) {

        if (omsMessageMember == null || omsMessageMember.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsMessageMember);

    }

    /**
     * 2-5-03批量查询成员消息接收[7530]
     * gen by moon at 10/14/2023, 11:31:28 PM
     */
    @Trace(operationName = "2-5-03批量查询成员消息接收")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageMember> batchQueryMemberMsgReceive(BatchQueryMemberMsgReceiveReq reqDto) {

        try {
            QueryWrapper<OmsMessageMember> queryWrapper = new QueryWrapper<>();
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            if (reqDto.getMemberMsgReceiveList() == null || reqDto.getMemberMsgReceiveList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("message_id", reqDto.getMemberMsgReceiveList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-03批量删成员消息接收[7606]
     * gen by moon at 10/26/2023, 10:59:55 PM
     */
    @Trace(operationName = "2-5-03批量删成员消息接收")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteMemberMsgReceive(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-5-03执行通道类型批量查成员消息接收[7978]
     * gen by moon at 11/19/2023, 12:00:02 AM
     */
    @Trace(operationName = "2-5-03执行通道类型批量查成员消息接收")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageMember> implementChannelTypeBatchQueryMemberMsgReceive(ImplementChannelTypeBatchQueryMemberMsgReceiveReq reqDto) {

        try {
            QueryWrapper<OmsMessageMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMemberMsgReceiveList() == null || reqDto.getMemberMsgReceiveList().isEmpty()) {
                return new ArrayList<>();
            }
            // TODO: 2023/11/20 手写代码 
            if (reqDto.getMessageImportantLevel() != null) {
                queryWrapper.ge("message_important_level", reqDto.getMessageImportantLevel());
                reqDto.setMessageImportantLevel(null);
            }
            queryWrapper.in("channel_type_code", reqDto.getMemberMsgReceiveList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsMessageMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-5-03批量查询成员接收消息 by身份人员[8186]
     * gen by moon at 12/23/2023, 1:16:58 AM
     */
    @Trace(operationName = "2-5-03批量查询成员接收消息 by身份人员")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsMessageMember> batchQueryMemberMsgReceiveByOriRoleMem(BatchQueryMemberMsgReceiveByOriRoleMemReq reqDto) {

        try {
            QueryWrapper<OmsMessageMember> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getMemberMsgReceiveList() == null || reqDto.getMemberMsgReceiveList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("original_role_member_id", reqDto.getMemberMsgReceiveList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsMessageMember.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsMessageMember.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsMessageMember.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsMessageMember>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

}

